package com.redrose.flutterdemo.camera;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.redrose.flutterdemo.R;
import com.redrose.flutterdemo.bean.Photo;
import com.redrose.flutterdemo.databinding.ActivityBigimageBinding;
import com.redrose.flutterdemo.pagetransformer.RotateDownPageTransformer;
import com.redrose.flutterdemo.ui.activitys.BaseActivity;
import com.redrose.flutterdemo.utils.GlideUtils;
import com.redrose.flutterdemo.view.ResizableImageView;

import org.litepal.crud.DataSupport;

import java.util.List;
@Route(path = "/activity/singleImage")
public class BigImageActivity extends BaseActivity<ActivityBigimageBinding> {
    private int mCurrentMax;
    private List<Photo> mPhotoList;
    private ImagePagerAdapter mAdapter;
    private int mPosition;
    private boolean isShowExtra = true;

    @Override
    public int setLayout() {
        return R.layout.activity_bigimage;
    }

    private void initEvent() {
        mBinding.imageViewPager.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

            }
        });
        mBinding.imageViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                refrashPosition(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void refrashPosition(int position) {
        position = position + 1;
        String statues = String.format("%d / %d", position, mCurrentMax);
        mBinding.imagePosition.setText(statues);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (isShowExtra) {
            isShowExtra = false;
        } else {
            isShowExtra = true;
        }
        showLayout();
        return false;
    }

    private void showLayout() {
        if (isShowExtra) {
            mBinding.titleLayout.setVisibility(View.VISIBLE);
            mBinding.bottomLayout.setVisibility(View.VISIBLE);
        } else {
            mBinding.titleLayout.setVisibility(View.GONE);
            mBinding.bottomLayout.setVisibility(View.GONE);
        }
    }

    private void initAdapter() {
        mAdapter = new ImagePagerAdapter();
        mBinding.imageViewPager.setAdapter(mAdapter);
        /** 翻页动画设置 */
        /*mViewPager.setPageTransformer(false,new ZoomOutPageTransformer());*/
        /*mViewPager.setPageTransformer(false,new DepthPageTransformer());*/
        mBinding.imageViewPager.setPageTransformer(false, new RotateDownPageTransformer());
        mBinding.imageViewPager.setCurrentItem(mPosition);
        refrashPosition(mPosition);
    }
    @Override
    public void initData() {

        Photo specifiefPhoto = (Photo) getIntent().getSerializableExtra("photoData");
        mPhotoList = DataSupport.order("classId desc").find(Photo.class);
        mCurrentMax = mPhotoList.size();
        getRealPosition(specifiefPhoto);
        initAdapter();
        initEvent();
    }

    private void getRealPosition(Photo p) {
        if (p == null) {
            mPosition = 0;
            return;
        }
        int size = mPhotoList.size();
        for (int i = 0; i < size; i++) {
            Photo photo = mPhotoList.get(i);
            if (p.getTime().equals(photo.getTime())) {
                mPosition = i;
                break;
            }
        }
    }

    private class ImagePagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {

            return mCurrentMax;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.image_viewpager_item, null);
            ResizableImageView imageView = view.findViewById(R.id.resize_image);
            GlideUtils.load(imageView, mPhotoList.get(position).getPath());
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
